package cn.lingyangwl.agile.misc.short_link.controller;

import cn.lingyangwl.agile.misc.short_link.model.entity.*;
import cn.lingyangwl.agile.misc.short_link.model.rqrs.*;
import cn.lingyangwl.agile.misc.short_link.service.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.framework.core.response.*;
import cn.lingyangwl.framework.validation.group.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 域名
 *
 * @author shenguangyang
 */
@Api(tags = "短链域名")
@RestController
@RequestMapping("/linkDomain")
public class DomainController {
    @Resource
    private DomainService domainService;

    @ApiOperation(value = "保存-域名")
    @PostMapping("/saveDomain")
    @RamResource(action = "sl:linkDomain:save", name = "保存短链域名", type = ActionType.WRITE)
    public Resp<?> saveDomain(@Validated @RequestBody DomainReq rq) {
        return Resp.ok(domainService.saveDomain(rq));
    }

    @ApiOperation(value = "更新-域名")
    @PostMapping("/updateDomain")
    @RamResource(action = "sl:linkDomain:update", name = "更新短链域名", type = ActionType.WRITE)
    public Resp<?> updateDomain(@Validated(UpdateGroup.class) @RequestBody DomainReq rq) {
        return Resp.ok(domainService.updateDomain(rq));
    }

    @ApiOperation(value = "批量删除-域名")
    @RamResource(action = "sl:linkDomain:delete", name = "删除短链域名", type = ActionType.WRITE)
    @DeleteMapping("/batchDeleteDomain")
    public Resp<?> batchDeleteDomain(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(domainService.deleteDomainByIds(ids));
    }

    @ApiOperation(value = "删除-域名")
    @RamResource(action = "sl:linkDomain:delete", name = "删除短链域名", type = ActionType.WRITE)
    @DeleteMapping("/deleteDomain")
    public Resp<?> deleteDomain(@RequestParam(value = "id") Long id) {
        return Resp.ok(domainService.deleteDomainByIds(Collections.singletonList(id)));
    }

    @ApiOperation(value = "查询列表-域名")
    @RamResource(action = "sl:linkDomain:query", name = "查询短链域名", type = ActionType.READ)
    @GetMapping("/listDomain")
    public Resp<List<LinkDomain>> listDomain(DomainListReq query) {
        return Resp.ok(domainService.listDomain(query));
    }
}
